package Twitter.RequestSender;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import twitter.RequestParser.XMLReader;
import twitter.RequestParser.XMLWriter;
import twitter.TwitterLibrary.SocketConfig;
import twitter.TwitterLibrary.Utility;
/***********************************************
 * this class is designed for sending request
 * @author fang
 *
 */
public class FrontendRequestSender extends RequestSender{
	
	
	private XMLWriter xmlWriter;
	private XMLReader xmlReader;
	
/**********************************************
 * Class constructor
 * @param xmlWriter
 * @param xmlReader
 */
	public FrontendRequestSender(XMLWriter xmlWriter,XMLReader xmlReader,Utility utility) {

		super(utility);
		this.xmlWriter = xmlWriter;
		this.xmlReader=xmlReader;
		
	}
/*******************************************************
 * send the search for version request to back end server 
 * from front end server
 * @param query
 * @return
 */
	public  int searchForVersion(String query) {
	
	
		String result = sendCommunicationRequest(xmlWriter
				.queryForVersion(query)+" \r\n end \r\n");
		
		Document DOC_response=null;
		try {
			
			DOC_response = utility.stringToDom(result);
			
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		 return xmlReader.readMaxVersionFromResponse(DOC_response);
		
		
	}
	/****************************************
	 * according the xml response get from the backend to create the twitter xml
	 *returntype=\"1\" return the status and version and tag
	 * @param tag
	 * @return
	 */
	
	public  HashMap<String, ArrayList<String>> searchStatusByTag(String tag)
	{
		
		String responses=sendCommunicationRequest(xmlWriter.queryForAll(tag)+"\r\n end \r\n");
		
		Document DOC_response=null;
		try {
			DOC_response = utility.stringToDom(responses);
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return xmlReader.readStatusFromResponse(DOC_response);
		
	}
	/*******************************************
	 * send the add new status request to back end
	 *  server from front end server
	 * <query httptype=\"POST\" status=\"newStatus\" ></query>
	 */
	public  String addNewStatus(String newStatus)
	{
		
		                             
			
			String queryString=xmlWriter.postNewStatus(newStatus)+"\r\n";
			queryString+="end \r\n";
			
		String responses= sendCommunicationRequest( queryString);
		
		Document DOC_response=null;
		try {
			DOC_response = utility.stringToDom(responses);
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		 return xmlReader.readResponseFromPost(DOC_response);
		
	}
	/********************************
	 * send request to backend server from front end server
	 * @param queryString
	 * @return
	 */
	private  String sendCommunicationRequest(String queryString) {
		
		return super.sendRequest(SocketConfig.primaryIp, queryString,
				SocketConfig.primaryPort);

	}
	
}
